package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.zxing.client.result.VCardResultParser;
import com.jwgf.common.ViewCache;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;


public class MachineStateMenuFragment extends MenuFragment implements
        OnClickListener {
    public static final String fragmentTag = "MachineStateMenuFragment";
    private Button runningStateBtn;
    private Button machineLogBtn;
    private Button machineStatisticBtn;
    private Button spindleStatusBtn;
    private Button spindleLogBtn;
    private Button spindleStatisticBtn;
    private Button cbfStatusBtn;
    private Button cbfLogBtn;
    private Button dofferStatusBtn;
    private Button dofferLogBtn;
    private Button directCurrentBtn;
    private Button faultNavigationBtn;
    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_machine_state_menu);
//        View v = inflater.inflate(R.layout.fragment_machine_state_menu,
//                container, false);

        runningStateBtn = v.findViewById(R.id.running_state_button);
        machineLogBtn = v.findViewById(R.id.machine_log_button);
        machineStatisticBtn = v.findViewById(R.id.machine_statistic_button);
        spindleStatusBtn = v.findViewById(R.id.spindle_status_button);
        spindleLogBtn = v.findViewById(R.id.spindle_log_button);
        spindleStatisticBtn = v.findViewById(R.id.spindle_statistic_button);
        cbfStatusBtn = v.findViewById(R.id.cbf_status_button);
        cbfLogBtn = v.findViewById(R.id.cbf_log_button);
        dofferStatusBtn = v.findViewById(R.id.doffer_status_button);
        dofferLogBtn = v.findViewById(R.id.doffer_log_button);
        directCurrentBtn = v.findViewById(R.id.direct_current_button);
        faultNavigationBtn = v.findViewById(R.id.fault_navigation_button);

        runningStateBtn.setSelected(true);

        runningStateBtn.setOnClickListener(this);
        machineLogBtn.setOnClickListener(this);
        machineStatisticBtn.setOnClickListener(this);
        spindleStatusBtn.setOnClickListener(this);
        spindleLogBtn.setOnClickListener(this);
        spindleStatisticBtn.setOnClickListener(this);
        cbfStatusBtn.setOnClickListener(this);
        cbfLogBtn.setOnClickListener(this);
        dofferStatusBtn.setOnClickListener(this);
        dofferLogBtn.setOnClickListener(this);
        directCurrentBtn.setOnClickListener(this);
        faultNavigationBtn.setOnClickListener(this);

        if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
            machineStatisticBtn.setVisibility(View.VISIBLE);
        } else {
            machineStatisticBtn.setVisibility(View.GONE);
        }

        if (sp.getValue(MachineParameterSharedPreference.MACHINE_DIRECT_CURRENT, "0").equals("0")) {
            directCurrentBtn.setVisibility(View.GONE);
        } else {
            directCurrentBtn.setVisibility(View.VISIBLE);
        }

        if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("0")) {
            cbfStatusBtn.setEnabled(false);
            cbfStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            cbfLogBtn.setEnabled(false);
            cbfLogBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferStatusBtn.setEnabled(false);
            dofferStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferLogBtn.setEnabled(false);
            dofferLogBtn.setTextColor(getResources().getColor(R.color.disable));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("1")) {
            cbfStatusBtn.setEnabled(false);
            cbfStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            cbfLogBtn.setEnabled(false);
            cbfLogBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferStatusBtn.setEnabled(true);
            dofferStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferLogBtn.setEnabled(true);
            dofferLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("4")) {
            cbfStatusBtn.setEnabled(false);
            cbfStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            cbfLogBtn.setEnabled(false);
            cbfLogBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferStatusBtn.setEnabled(true);
            dofferStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferLogBtn.setEnabled(true);
            dofferLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else {
            cbfStatusBtn.setEnabled(true);
            cbfStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            cbfLogBtn.setEnabled(true);
            cbfLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferStatusBtn.setEnabled(true);
            dofferStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferLogBtn.setEnabled(true);
            dofferLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        }

        return v;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("0")) {
            cbfStatusBtn.setEnabled(false);
            cbfStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            cbfLogBtn.setEnabled(false);
            cbfLogBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferStatusBtn.setEnabled(false);
            dofferStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferLogBtn.setEnabled(false);
            dofferLogBtn.setTextColor(getResources().getColor(R.color.disable));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("1")) {
            cbfStatusBtn.setEnabled(false);
            cbfStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            cbfLogBtn.setEnabled(false);
            cbfLogBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferStatusBtn.setEnabled(true);
            dofferStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferLogBtn.setEnabled(true);
            dofferLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("4")) {
            cbfStatusBtn.setEnabled(false);
            cbfStatusBtn.setTextColor(getResources().getColor(R.color.disable));
            cbfLogBtn.setEnabled(false);
            cbfLogBtn.setTextColor(getResources().getColor(R.color.disable));
            dofferStatusBtn.setEnabled(true);
            dofferStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferLogBtn.setEnabled(true);
            dofferLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else {
            cbfStatusBtn.setEnabled(true);
            cbfStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            cbfLogBtn.setEnabled(true);
            cbfLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferStatusBtn.setEnabled(true);
            dofferStatusBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            dofferLogBtn.setEnabled(true);
            dofferLogBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        }
        if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
            machineStatisticBtn.setVisibility(View.VISIBLE);
        } else {
            machineStatisticBtn.setVisibility(View.GONE);
        }
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_DIRECT_CURRENT, "0").equals("0")) {
            directCurrentBtn.setVisibility(View.GONE);
        } else {
            directCurrentBtn.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void switchSelection(int id) {
        if (id == R.id.running_state_button) {
            runningStateBtn.setSelected(true);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.machine_log_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(true);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.machine_statistic_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(true);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.spindle_status_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(true);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.spindle_log_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(true);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.spindle_statistic_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(true);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.cbf_status_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(true);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.cbf_log_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(true);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.doffer_status_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(true);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.doffer_log_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(true);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.direct_current_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(true);
            faultNavigationBtn.setSelected(false);
        } else if (id == R.id.fault_navigation_button) {
            runningStateBtn.setSelected(false);
            machineLogBtn.setSelected(false);
            machineStatisticBtn.setSelected(false);
            spindleStatusBtn.setSelected(false);
            spindleLogBtn.setSelected(false);
            spindleStatisticBtn.setSelected(false);
            cbfStatusBtn.setSelected(false);
            cbfLogBtn.setSelected(false);
            dofferStatusBtn.setSelected(false);
            dofferLogBtn.setSelected(false);
            directCurrentBtn.setSelected(false);
            faultNavigationBtn.setSelected(true);
        }
    }

    @Override
    public void onClick(View v) {
        SwitchPageManager.getInstance().switchSecondMenu(getFragmentManager(), v.getId());
    }
}
